package com.sdry.product.prod.controller;
import	java.util.ArrayList;

import com.sdry.basic.entity.Container;
import com.sdry.basic.entity.Material;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.common.BaseController;
import com.sdry.common.BaseTypeEnum;
import com.sdry.common.ResultAppInfo;
import com.sdry.exception.BizException;
import com.sdry.product.prod.entity.ProductHandleOut;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.service.ProductHandleOutService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 前处理区app端
 * @Author chenBin
 * @Date 2020-04-23 17:32
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "preTreatment")
public class ProductHandleAppController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private ProductHandleOutService productHandleOutService;

    /**
     * 扫码入前处理区
     * [{"psnumber":"20-11", "psId":"21","list":[{"maId":"15","quantity":"298",
     *       "userQuantity":"300","maSprayingRequirements":"银色", "urgent":"紧急","remark04":"WZZ" } ]}]
     */
    @RequestMapping(value = "/api/addPreTreatment")
    public ResultAppInfo addPreTreatment(String json, String userId) {
        logger.info("扫码入前处理区",json);
        try {
            productHandleOutService.addPreTreatment(json,userId);
            return getResultAppInfo("1",null,"");
        }catch (BizException e){
            return getResultAppInfo("0",null, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return getResultAppInfo("0",null, BaseAPIMessageTypeEnum.A001.getName());
        }
    }

    /**
     * 扫码出前处理区
     * [{"psnumber":"20-11", "psId":"21","list":[{"maId":"15","quantity":"298",
     *       "userQuantity":"300","maSprayingRequirements":"银色", "urgent":"紧急","remark04":"WZZ" } ]}]
     */
    @RequestMapping(value = "/api/addPreTreatmentOut")
    public ResultAppInfo addPreTreatmentOut(String json, String userId) {
        logger.info("转运--扫码出前处理区 json = {}"+json);
        try {
            productHandleOutService.addPreTreatmentOut(json,userId);
        }catch (BizException e){
            return getResultAppInfo("0",null, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return getResultAppInfo("0",null, e.getMessage());
        }
        return getResultAppInfo("1",null,"");
    }


    /**
     * 查询中转区以及分拣区框编号1
     * 查询装载表所有的容器
     */
    @RequestMapping("/api/queryTransitAreaNumber")
    public ResultAppInfo queryTransitAreaNumber() {
        logger.info("查询中转区以及分拣区框编号1");
        List<Container> containerList = productHandleOutService.queryTransitAreaNumber();
        return getResultAppInfo("1",containerList,"");
    }

    /**
     * 查询中转区以及分拣区框编号2
     * 查询装载表所有的容器
     */
    @RequestMapping("/api/queryTransitAreaNumberByInfo")
    public ResultAppInfo queryTransitAreaNumberByInfo() {
        logger.info("查询中转区以及分拣区框编号2");
        //List<Container> containerList = productHandleOutService.queryTransitAreaNumberByInfo();
        List<Container> containerList = productHandleOutService.queryTransitAreaNumber();
        return getResultAppInfo("1",containerList,"");
    }

    /**
     * 查询前处理区库存编号
     */
    @RequestMapping("/api/queryProductHandleNumberByInfo")
    public ResultAppInfo queryProductHandleNumberByInfo() {
        logger.info("查询前处理区库存编号");
        List<Container> containerList = productHandleOutService.queryProductHandleNumberByInfo();
        return getResultAppInfo("1",containerList,"");
    }

    /**
     * 查询前处理区物料信息
     * @return
     */
    @RequestMapping(value = "/api/queryProductHandleMateriel")
    public ResultAppInfo  queryProductHandleMateriel(String psnumber) {
        logger.info("询前处理区物料信息","psnumber = " +psnumber);
        //为空
        if (psnumber == null) {
            return getResultAppInfo("0",null,BaseAPIMessageTypeEnum.A001.getName());
        }
        List<ResultAppList> resultAppListList = productHandleOutService.queryProductHandleMateriel(psnumber);
        return getResultAppInfo("1",resultAppListList,"");
    }

    /**
     * 转运查询前处理区物料信息
     * @return
     */
    @RequestMapping(value = "/api/queryProductHandleTransportMateriel")
    public ResultAppInfo  queryProductHandleTransportMateriel(String psnumber) {
        logger.info("转运查询前处理区物料信息","psnumber = " +psnumber);
        //为空
        if (psnumber == null) {
            return getResultAppInfo("0",null,BaseAPIMessageTypeEnum.A001.getName());
        }
        List<ResultAppList> resultAppListList = productHandleOutService.queryProductHandleTransportMateriel(psnumber);
        return getResultAppInfo("1",resultAppListList,"");
    }



    /**
     * 区域查询（流程）
     * @param request 请求
     * @return Map<String,Object>
     */
    @RequestMapping(value = "/api/queryAddress")
    public Map<String,Object> queryAddress(HttpServletRequest request) {
        JSONArray json = new JSONArray();

        String qcl2 = BaseTypeEnum.getNameByCode("YG");
        String qcl3 = BaseTypeEnum.getNameByCode("PT");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("wrlnumber",qcl2);
        jsonObject2.put("code","YG");

        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("wrlnumber",qcl3);
        jsonObject3.put("code","PT");

        json.add(jsonObject2);
        json.add(jsonObject3);

        Map<String,Object> map = new HashMap<>(16);
        map.put("status",1);
        map.put("message","");
        map.put("address",json);
        map.put("token",request.getParameter("token"));
        return map;
    }
}
